//
//  DDIDCardInputView.m
//  GSDache
//
//  Created by guoshikeji_a on 2018/1/26.
//  Copyright © 2018年 tanyin. All rights reserved.
//

#import "DDCustomNumberInputView.h"

#import "DDCustomNumberInputLabelView.h"

#import "DDCustomNumberInputtUndoButton.h"

#define DDIDCardInputViewInputStringChar @"123456789.0"

#define DDIDCardInputViewNumberViewBaseTag 1000

@interface DDCustomNumberInputView ()

@property (nonatomic, strong) NSMutableArray *numberViewArray;


/**
 *deleteButton
 */
@property (nonatomic, strong) DDCustomNumberInputtUndoButton *deleteButton;

@end

@implementation DDCustomNumberInputView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self initViews];
    }
    return self;
}
- (void)setTextField:(UITextField *)textField {
    _textField = textField;
    //
    if (_textField) {
        _textField.inputView = self;
    }
}

- (void)initViews {
    NSString *charString = DDIDCardInputViewInputStringChar;
    CGFloat width = (self.frame.size.width-2*DDIDCardInputLabelViewPadding)/3;
    CGFloat height = (self.frame.size.height-Height_Bottom_SafeArea-DDIDCardInputLabelViewPadding)/4;
    self.numberViewArray = [NSMutableArray arrayWithCapacity:charString.length];
    for (int i=0; i<charString.length; i++) {
        NSInteger row = i%3;
        NSInteger line = i/3;
        DDCustomNumberInputLabelView *labelView = [DDCustomNumberInputLabelView new];
        labelView.numberText = [charString substringWithRange:NSMakeRange(i, 1)];
        labelView.frame = CGRectMake(DDIDCardInputLabelViewPadding+row*width, DDIDCardInputLabelViewPadding+line*height, width, height);
        labelView.touchedSelf = false;
        [self addSubview:labelView];
        [self.numberViewArray addObject:labelView];
    }
    //delete
    self.deleteButton = [DDCustomNumberInputtUndoButton new];
    [self.deleteButton setImage:[UIImage imageNamed:@"DDIDCardInputDeleteImageNormal"] forState:UIControlStateNormal];
    [self.deleteButton setImage:[UIImage imageNamed:@"DDIDCardInputDeleteImageHighlighted"] forState:UIControlStateHighlighted];
    [self addSubview:self.deleteButton];
    self.deleteButton.frame = CGRectMake(self.frame.size.width-width-DDIDCardInputLabelViewPadding, self.frame.size.height-height-Height_Bottom_SafeArea, width, height);
    [self.deleteButton addTarget:self action:@selector(deleteButtonTouchDownAction) forControlEvents:UIControlEventTouchDown];
    [self.deleteButton addLongPressUndoTarget:self action:@selector(deleteButtonTouchDownAction)];
}


#pragma mark - UITouch
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [[UIDevice currentDevice] playInputClick];
    //
    UITouch *touch = touches.anyObject;
    CGPoint touchPoint = [touch locationInView:self];
    [self changeTouchViewWithPoint:touchPoint];
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    UITouch *touch = touches.anyObject;
    CGPoint touchPoint = [touch locationInView:self];
    [self changeTouchViewWithPoint:touchPoint];
}
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesCancelled:touches withEvent:event];
    [self resetNumberViews];
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    UITouch *touch = touches.anyObject;
    CGPoint touchPoint = [touch locationInView:self];
    [self touchEndWithPoint:touchPoint];
}
/**
 当touch改变时 改变touch对象
 */
- (void)changeTouchViewWithPoint:(CGPoint)point {
    for (UIView *view in self.numberViewArray) {
        DDCustomNumberInputLabelView *labelView = (DDCustomNumberInputLabelView *)view;
        labelView.touchedSelf = CGRectContainsPoint(labelView.frame, point);
    }
}
- (void)touchEndWithPoint:(CGPoint)point {
    for (UIView *view in self.numberViewArray) {
        DDCustomNumberInputLabelView *labelView = (DDCustomNumberInputLabelView *)view;
        labelView.touchedSelf = false;
        if (CGRectContainsPoint(view.frame, point)) {
            self.textField.text = [self.textField.text stringByAppendingString:labelView.numberText];
            [[NSNotificationCenter defaultCenter] postNotificationName:UITextFieldTextDidChangeNotification object:self.textField];
        }
    }
}


#pragma mark - 重置
- (void)resetNumberViews {
    for (UIView *view in self.numberViewArray) {
        DDCustomNumberInputLabelView *labelView = (DDCustomNumberInputLabelView *)view;
        labelView.touchedSelf = false;
    }
}

#pragma mark - deleteButtonTouchDownAction
- (void)deleteButtonTouchDownAction {
    [self deleteAction];
}
/**
 删除输入
 */
- (void)deleteAction {
    if (self.textField.text.length) {
        NSString *text = self.textField.text;
        self.textField.text = [text substringToIndex:text.length-1];
        [[NSNotificationCenter defaultCenter] postNotificationName:UITextFieldTextDidChangeNotification object:self.textField];
    }
}



+ (CGSize)defaultSize {
    return CGSizeMake(ScreenWidth, ScreenWidth*(432.0/750.0)+Height_Bottom_SafeArea);
}
+ (CGRect)defaultBounds {
    CGSize size = [self defaultSize];
    return CGRectMake(0, 0, size.width, size.height);
}

@end
